package com.hbt.word_provider.utils;

public class MatchStrUtil {
    public static int matcherStr(String src, String tgt){
        char[] src_c = src.toCharArray();
        char[] tgt_c = tgt.toCharArray();
        int src_len = src_c.length;
        int tgt_len = tgt_c.length;
        int max_count = 0;
        //移动次数
        int count = Math.abs(src_len-tgt_len);
        //确定src 和 tgt 谁大谁小
        char[] big_c;
        char[] small_c;
        if(src_len > tgt_len){
            big_c = src_c;
            small_c = tgt_c;
        }else {
            big_c = tgt_c;
            small_c = src_c;
        }
//        System.out.println("big:"+big_c.length+"  small:"+small_c.length+" count:"+count);
        //src小  tgt大
        for(int i = 0;i < count+1;i++){
            //第i次移动
            int match_count = 0;//第i次移动 匹配字符数
            for (int j = 0;j < small_c.length;j++){
                //第j个字符
                if(small_c[j] == big_c[j+i]){
                    //匹配
//                    System.out.println(small_c[j]+":"+big_c[j]+" index: "+j);
                    match_count++;
                }else {
                    //不匹配
//                    System.out.println("不相等index："+j);
                }
            }
            //取最大匹配数
            max_count = match_count>max_count?match_count:max_count;
//            System.out.println("第"+(i+1)+"次"+"结束  "+"匹配字符数："+match_count);
//            System.out.println("");
//            System.out.println("");
        }//for 循环结束

        //进行缩减对比匹配
        for(int k = 0; k < small_c.length-1;k++){//第k次缩减对比
            int match_count = 0;
            for(int i = small_c.length-2-k;i >= 0; i--){
                char big_char = big_c[big_c.length-1+(i-small_c.length+2+k)];
                if(small_c[i] == big_char){
//                    System.out.println(small_c[i]+":"+big_char);
//                    System.out.println("缩减对比匹配++++");
                    match_count++;
                }else {
//                    System.out.println(small_c[i]+":"+big_char);
//                    System.out.println("缩减对比字符不匹配---:"+i);
                }
            }
            max_count = match_count>max_count?match_count:max_count;
//            System.out.println("");System.out.println("");
//            System.out.println("第"+k+"次缩减对比结束："+match_count);
//            System.out.println(""); System.out.println("");
        }
//        System.out.println("最大匹配字符数："+max_count);
        float match_rate = (float)max_count/(float) small_c.length;
        int match_rate_int = ((int)(match_rate*100));
//        System.out.println("匹配率："+match_rate_int+"%");
        return match_rate_int;
    }//matcherStr end
}
